﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace RazorTemplateEngine
{
    public class ImportNamespaceResolver : ConfigurationSection
    {
        [ConfigurationProperty("",IsDefaultCollection=true)]
        public NamespaceCollection ImportNamespaces
        {
            get { return (NamespaceCollection)base[""]; }
        }

        public IEnumerable<Namespace> GetConfigNamespaces()
        {
            ImportNamespaceResolver mysec;
            try
            {
                mysec = (ImportNamespaceResolver)ConfigurationManager.GetSection("RazorTemplateEngineImportNamespace");
            }
            catch
            {
                throw new ConfigurationErrorsException("Missing configuration!");
            }
            foreach (Namespace ele in mysec.ImportNamespaces)
                yield return ele;

        }

    }

    [ConfigurationCollection(typeof(Namespace))]
    public class NamespaceCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new Namespace();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((Namespace)element).NamespaceName;
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }

        public Namespace this[int index]
        {
            get{
                return (Namespace)BaseGet(index);
            }
        }

        protected override string ElementName
        {
            get
            {
                return "add";
            }
        }
    }

    public class Namespace : ConfigurationElement
    {
        [ConfigurationProperty("namespace", DefaultValue = "", IsRequired = true)]
        public string NamespaceName
        {
            get { return (string)base["namespace"]; }
        }
    }
}
